package com.sprocomm.agingtest;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Debug;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/6/12.
 */

public class Tools {
    /**
     * 设置屏幕亮度
     */
    public static void setScreenBrightness(float brightness, Activity activity) {
        Window window = activity.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.screenBrightness = brightness;
        window.setAttributes(layoutParams);
    }

    /**
     * 测试时候退出提示
     */
    public static AlertDialog exitNotic(final Activity activity) {
        AlertDialog alertDialog = new AlertDialog.Builder(activity)
                .setTitle("Determine exit testing?")
                .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        activity.finish();
                    }
                })
                .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //什么都不做
                    }
                })
                .create();
        alertDialog.show();
        return alertDialog;

    }

    /**
     * 点击pass或者fail退出提示
     */
    public static void exitResultNotic(final Activity activity, final boolean result, String notice) {
        final Intent intent = new Intent();
        intent.putExtra("result", result);
        if (result) {
            activity.setResult(activity.RESULT_OK, intent);
            activity.finish();
        } else {
            new AlertDialog.Builder(activity)
                    .setTitle(notice)
                    .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            activity.setResult(activity.RESULT_OK, intent);
                            activity.finish();
                        }
                    })
                    .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //什么都不做
                        }
                    })
                    .create().show();
        }
    }

    /**
     * 点击pass或者fail退出提示
     */
    public static void exitResultNotic(final Activity activity, final boolean result) {
        if (result) {
            Intent intent = new Intent();
            intent.putExtra("result", result);
            activity.setResult(activity.RESULT_OK, intent);
            activity.finish();
        } else {
            new AlertDialog.Builder(activity)
                    .setTitle("Determine exit testing?")
                    .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent();
                            intent.putExtra("result", result);
                            activity.setResult(activity.RESULT_OK, intent);
                            activity.finish();
                        }
                    })
                    .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //什么都不做
                        }
                    })
                    .create().show();
        }

    }

    /**
     * fail测试退出
     */
    public static void exitFail(Activity activity){
        Intent intent = new Intent();
        intent.putExtra("result", false);
        activity.setResult(activity.RESULT_OK, intent);
        activity.finish();
    }

    /**
     * 把long时间转换成易识别时间string
     */
    public static String longTime2String(long time) {
        long second = time / 1000;
        int h = Integer.parseInt(String.valueOf(second / 3600));
        int m = Integer.parseInt(String.valueOf((second % 3600) / 60));
        int s = Integer.parseInt(String.valueOf(second % 60));
        return h + ":" + m + ":" + s;
    }

    /**
     * 获取屏幕宽度
     */
    public static int getWinWigth(WindowManager windowManager) {
        Point point = new Point();
        windowManager.getDefaultDisplay().getSize(point);
        return point.x;
    }

    /**
     * 获取屏幕高度
     */
    public static int getWinHeight(WindowManager windowManager) {
        Point point = new Point();
        windowManager.getDefaultDisplay().getSize(point);
        return point.y;
    }

    /**
     * 包装的LOG方法
     * 自动获取报错路径
     */
    public static void logE(String msg) {
        String customTagPrefix = "";
        StackTraceElement caller = Thread.currentThread().getStackTrace()[4];
        String tag = "%s.%s(L:%d)";
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
        tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;

        if (Config.DEBUG) {
            Log.e(tag, msg);
        }
    }
    /**
     * 包装的LOG方法
     * 自动获取报错路径
     */
    public static void logE(String tag,String msg) {
        if (Config.DEBUG) {
            Log.e(tag, msg);
        }
    }

    /**
     * 获取网络类型 -100为获取失败
     * @param c
     * @return
     */
    public static int getConnectyType(Context c) {
        ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni != null && ni.isConnected()){
           return ni.getType();
        }else{
            return -100;
        }
    }

    public static void showToast(Context context,String msg){
        Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
    }

    public static void setMargins (View v, int l, int t, int r, int b) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            p.setMargins(l, t, r, b);
            v.requestLayout();
        }
    }
}
